﻿using BusinessLogic;
using Common;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace AppAdmin.Controllers
{
    public class AccountController : Controller
    {
        //
        // GET: /Account/

        public ActionResult LogOn()
        {
            return View();
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public string LogOn(FormCollection collection)
        {
            string LoginName = collection["LoginName"].Trim();
            string Password = collection["Password"].Trim();
            string CheckCode = collection["CheckCode"].Trim();
            KeyValuePair<int, string> result;
            if (string.IsNullOrEmpty(LoginName))
            {
                result = new KeyValuePair<int, string>(0, "用户名不能为空");
            }
            else if (string.IsNullOrEmpty(Password))
            {
                result = new KeyValuePair<int, string>(0, "密码不能为空");
            }
            else if (Session["CheckCode"] == null || string.Compare(CheckCode, Session["CheckCode"].ToString(), true) != 0)
            {
                result = new KeyValuePair<int, string>(0, "验证码不正确");
            }
            else
            {
               // Model1Container db = new Model1Container();
              //  db.sp_Sys_AdminUserLogOn(LoginName, FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "MD5"), Request.UserHostAddress);
            }
            return "";

        }

        public string IsLogin()
        {
            bool isLogin = true;
            if (string.IsNullOrEmpty(ThisAdminSession.LoginName))
            {
                isLogin = false;
            }
            string strJson = "";// LigerUIHelper.ParseToJson(isLogin);
            return strJson;
        }

        public ActionResult LogOut()
        {
            Session.Clear();
            Session.Abandon();
            return RedirectToAction("LogOn", "Account");
        }
        public ActionResult GetAuthCode()
        {
            return File(new VerifyCode().GetVerifyCode(), @"image/Gif");
        }

        [HttpPost]
        public ActionResult CheckLogin(string username, string password, string code)
        {
            try
            {
                string LoginName = username.Trim();
                string Password = password.Trim();
                string CheckCode = code.Trim();
                KeyValuePair<int, string> result;
                if (string.IsNullOrEmpty(LoginName))
                {
                    result = new KeyValuePair<int, string>(0, "用户名不能为空");
                }
                else if (string.IsNullOrEmpty(Password))
                {
                    result = new KeyValuePair<int, string>(0, "密码不能为空");
                }
                else if (Session["CheckCode"] == null || string.Compare(CheckCode, Session["CheckCode"].ToString(), true) != 0)
                {
                    result = new KeyValuePair<int, string>(0, "验证码不正确");
                }
                else
                {
                    DataSet ds = new LoginLogic().LogOn(LoginName, FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "MD5"), Request.UserHostAddress);
                    switch (ds.Tables[0].Rows[0]["LoginState"].ToString())
                    {
                        case "1":
                            {
                                //if (!bool.Parse(ds.Tables[1].Rows[0]["State"].ToString()))
                                //{
                                    result = new KeyValuePair<int, string>(1, "登录成功");
                                    Session["CheckCode"] = null;
                                    DataRow dr = ds.Tables[1].Rows[0];
                                    ThisAdminSession.UserId = int.Parse(dr["UserId"].ToString());
                                    ThisAdminSession.LoginName = dr["LoginName"].ToString();
                                    ThisAdminSession.RealName = dr["RealName"].ToString();
                                    //ThisSession.UserFunRight = ModelConvertHelper.TableToList<UserFunRight>(B_Sys_FunRight.GetInstance().GetUserFunRight(dr["UserId"].ToString()).Tables[0]).ToList();
                                    //ThisSession.UserModule = ModelConvertHelper.TableToList<UserModule>(B_Sys_Module.GetInstance().GetUserModule(dr["UserId"].ToString()).Tables[0]).ToList();
                                    ThisAdminSession.IsAdmin =dr["IsAdmin"].ToString()=="1"?true:false;
                                //}
                                ////else
                                ////{
                                ////    result = new KeyValuePair<int, string>(0, "当前账号已被冻结，请联系管理员");
                                //}
                                break;
                            }
                        case "2":
                            {
                                result = new KeyValuePair<int, string>(0, "连续5次密码错误,已被限制登录10分钟");
                                break;
                            }
                        case "3":
                            {
                                result = new KeyValuePair<int, string>(0, "用户名或密码错误");//密码错误
                                break;
                            }
                        case "4":
                            {
                                result = new KeyValuePair<int, string>(0, "用户名或密码错误");//用户名错误
                                break;
                            }
                        case "5":
                            {
                                result = new KeyValuePair<int, string>(0, "账号被冻结，请联系管理员");//用户名错误
                                break;
                            }
                        default:
                            {
                                result = new KeyValuePair<int, string>(0, "用户名或密码错误");
                                break;
                            }
                    }

                }
                if (result.Key == 1)
                {
                    return Content(new AjaxResult { state = ResultType.success.ToString(), message = "登录成功。" }.ToJson());
                }
                else
                {
                    return Content(new AjaxResult { state = ResultType.error.ToString(), message = result.Value }.ToJson());
                }

            }
            catch (Exception ex)
            {
                return Content(new AjaxResult { state = ResultType.error.ToString(), message = ex.Message }.ToJson());
            }
        }
    }
}
